package au.edu.jcu.it.numberguess;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class HighScoresOpenHelper extends SQLiteOpenHelper {
	long numRows1, numRows2, numRows3;

	public HighScoresOpenHelper(Context context) {
		super(context, "HighScores", null, 10);

	}

	public HighScoresOpenHelper(Context context, String name,
			CursorFactory factory, int version,
			DatabaseErrorHandler errorHandler) {
		super(context, name, factory, version, errorHandler);

	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		setup(database);

	}

	private void setup(SQLiteDatabase database) {
		database.execSQL("DROP TABLE IF EXISTS BeatClock;");
		database.execSQL("CREATE TABLE BeatClock"
				+ " (ID TEXT,name TEXT, score INTEGER, datetime TEXT);");
		database.execSQL("DROP TABLE IF EXISTS AppGuess;");
		database.execSQL("CREATE TABLE AppGuess"
				+ " (ID TEXT,name TEXT, score TEXT, datetime TEXT);");
		database.execSQL("DROP TABLE IF EXISTS TimeTrial;");
		database.execSQL("CREATE TABLE TimeTrial"
				+ " (ID TEXT, name TEXT, score INTEGER, datetime TEXT);");
		numRows1 = DatabaseUtils.longForQuery(database,
				"SELECT COUNT(*) FROM BeatClock", null) + 1;
		numRows2 = DatabaseUtils.longForQuery(database,
				"SELECT COUNT(*) FROM TimeTrial", null) + 1;
		numRows3 = DatabaseUtils.longForQuery(database,
				"SELECT COUNT(*) FROM AppGuess", null) + 1;

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		setup(db);

	}

}
